/**
 *	计划任务
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-25
 */

package com.framework.Model;


import com.framerock.db.DBConn;
import com.framerock.server.Request;
import com.framerock.baselib.CDate;
import com.framerock.baselib.CLog;
import com.framerock.baselib.Rock;
import com.framework.Flow.Flow;

import java.util.ArrayList;
import java.util.Map;
import com.framework.ACore.Model;


public class ModelRemind extends Model {

    public ModelRemind(DBConn dbs) {
        super(dbs);
        this.setTable("remind");
    }

    public static ModelRemind get(DBConn dbs)
    {
        return new ModelRemind(dbs);
    }

    private String now;
    private long nowlong;

    /**
     * 获取1分钟内运行的列表
     * */
    public ArrayList<Map<String, String>> getRunList()
    {
        ArrayList<Map<String, String>> rows = this.getAll("`status`=1 AND (`startdt` IS NULL OR `startdt`< NOW()) AND (`enddt` IS NULL OR `enddt`> NOW())");
        this.now        = CDate.now("Y:m:d:H:i:s:w", "");
        this.nowlong    = Long.parseLong(CDate.gettime());
        Map<String, String> da;
        ArrayList<Map<String, String>> data = Rock.getMapList();
        if(rows != null)for(int i=0;i<rows.size();i++){
            da      = rows.get(i);
            String runtime = this.runtime(da);
            if(!Rock.isEmpt(runtime)) {
                long ltime  = Long.parseLong(CDate.now("time", runtime));
                long jg     = ltime - this.nowlong;
                if(jg >= -5000 && jg<= 60*1000){
                    da.put("runtimestr", runtime);
                    da.put("runtime", CDate.gettimes(runtime));
                    data.add(da);
                }
            }
        }
        ModelOption.get(this.nDb).setVal("tasklast", CDate.now());
        return data;
    }


    //获取运行的时间
    public String runtime(Map<String, String> rs)
    {
        String rate     = Rock.getMapString(rs, "rate");
        String rateval  = Rock.getMapString(rs, "rateval");
        String str      = "";
        String arr[]    = this.now.split(":");
        String Y,m,d,H,i,s,w;
        Y   = arr[0];
        m   = arr[1];
        d   = arr[2];
        H   = arr[3];
        i   = arr[4];
        s   = arr[5];
        w   = arr[6];
        String date = ""+Y+"-"+m+"-"+d+"",val;

        if(rate.equals("o")) {
            if (rateval.indexOf(date) == 0) str = rateval;
        }else if(rate.equals("i")){
            str = ""+date+" "+H+":"+i+":"+rateval+"";
        }else if(rate.equals("h")){
            str = ""+date+" "+H+":"+rateval+"";
        }else if(rate.equals("w"+w+"")){
            str = ""+date+" "+rateval+"";
        }else if(rate.equals("y")){
            val = ""+m+"-"+d+"";
            if(rateval.indexOf(val)==0)str = ""+Y+"-"+rateval+"";
        }else if(rate.equals("m")){
            val = ""+d+"";
            if(rateval.indexOf(val)==0)str = ""+Y+"-"+m+"-"+rateval+"";
        }else if(rate.equals("g")){
            str = ""+date+" "+rateval+"";
        }else if(rate.equals("d")){
            str = ""+date+" "+rateval+"";
        }

        return str;
    }

    //运行
    public String runstart(Map<String, String> rs)
    {
        String str   = "success",val;
        String mknum = rs.get("mknum");
        String mid   = rs.get("mid");
        String id    = rs.get("id");
        String rate  = rs.get("rate");
        int cid      = Rock.getMapint(rs, "cid");
        int aid      = Rock.getMapint(rs, "aid");
        Boolean ebo  = true;
        Map<String, String> crs,ars;

        Request req  = new Request(null);
        if(cid > 0){
            crs = ModelCompany.get(this.nDb).getOne("`id`="+cid+" AND `status`=1");
            if(crs == null){
                this.update("`status`=0", id);
                ebo = false;
                str = "company not found";
            }else {
                req.setCompanyinfo(crs);
            }
        }

        if(ebo && aid > 0){
            ars = ModelUsera.get(this.nDb).getOne("`id`="+aid+"");
            req.setUserainfo(ars);
        }

        if(ebo) {
            Flow flow = Flow.get(this.nDb, mknum, req);
            flow.initData(mid);
            if (flow.isError()) {
                str = flow.getError();
            }else {
                String receid = rs.get("receid");
                if (Rock.isEmpt(receid)) receid = aid + "";
                Map<String, String> da = flow.remindRuning();
                String title = rs.get("title");
                String mess  = rs.get("mess");
                if(da != null) {
                    val = Rock.getMapString(da, "result");
                    if(!Rock.isEmpt(val))str = val;
                    val = Rock.getMapString(da, "receid");
                    if(val.equals("none"))receid = "";
                    val = Rock.getMapString(da, "title");
                    if(!Rock.isEmpt(val))title = val;
                    val = Rock.getMapString(da, "mess");
                    if(!Rock.isEmpt(val))mess = val;
                }
                if(!Rock.isEmpt(receid))flow.addTodo(receid, title, mess);
            }
        }
        req = null;
        Map<String, String> upda = Rock.getMap();
        upda.put("lastdt", CDate.now());
        upda.put("lastcont", str);
        if(Rock.equals(rate, "o"))upda.put("status", "0"); //就一次就停用
        this.update(upda, id);
        return str;
    }

    /**
     * 获取重复的时间
     * */
    public ArrayList<Map<String, String>> getRateList(String mknum, String mid, String dt)
    {
        String where = "`mknum`='"+mknum+"' AND `mid`="+mid+"";
        where += " AND `status`=1";
        where += " AND (`startdt` IS NULL OR `startdt`<='"+dt+" 23:59:59')";
        where += " AND (`enddt` IS NULL OR `enddt` >= '"+dt+" 00:00:00')";
        String arr[]    = dt.split("-");
        this.now        = CDate.now(""+arr[0]+":"+arr[1]+":"+arr[2]+":H:i:s:w", "");
        ArrayList<Map<String, String>> rows = this.getAll(where),data = Rock.getMapList();
        Map<String, String> rs;
        if(rows != null)for(int i=0;i<rows.size();i++){
            rs = rows.get(i);
            String time = this.runtime(rs);
            if(!Rock.isEmpt(time)) {
                rs.put("time", time);
                data.add(rs);
            }
        }
        return data;
    }
}
